package com.ruoyi.insurance.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.insurance.domain.Emergency;
import com.ruoyi.insurance.mapper.WebSafeTroubleMapper;
import com.ruoyi.insurance.domain.WebSafeTrouble;
import com.ruoyi.insurance.service.IWebSafeTroubleService;

/**
 * 网络安全事故Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-16
 */
@Service
public class WebSafeTroubleServiceImpl implements IWebSafeTroubleService 
{
    @Autowired
    private WebSafeTroubleMapper webSafeTroubleMapper;

    /**
     * 查询网络安全事故
     * 
     * @param troubleId 网络安全事故主键
     * @return 网络安全事故
     */
    @Override
    public WebSafeTrouble selectWebSafeTroubleByTroubleId(Integer troubleId)
    {
        return webSafeTroubleMapper.selectWebSafeTroubleByTroubleId(troubleId);
    }

    /**
     * 查询网络安全事故列表
     * 
     * @param webSafeTrouble 网络安全事故
     * @return 网络安全事故
     */
    @Override
    public List<WebSafeTrouble> selectWebSafeTroubleList(WebSafeTrouble webSafeTrouble)
    {
        return webSafeTroubleMapper.selectWebSafeTroubleList(webSafeTrouble);
    }

    /**
     * 新增网络安全事故
     * 
     * @param webSafeTrouble 网络安全事故
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWebSafeTrouble(WebSafeTrouble webSafeTrouble)
    {
        int rows = webSafeTroubleMapper.insertWebSafeTrouble(webSafeTrouble);
        insertEmergency(webSafeTrouble);
        return rows;
    }

    /**
     * 修改网络安全事故
     * 
     * @param webSafeTrouble 网络安全事故
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWebSafeTrouble(WebSafeTrouble webSafeTrouble)
    {
        webSafeTroubleMapper.deleteEmergencyByTroubleId(webSafeTrouble.getTroubleId());
        insertEmergency(webSafeTrouble);
        return webSafeTroubleMapper.updateWebSafeTrouble(webSafeTrouble);
    }

    /**
     * 批量删除网络安全事故
     * 
     * @param troubleIds 需要删除的网络安全事故主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWebSafeTroubleByTroubleIds(Integer[] troubleIds)
    {
        webSafeTroubleMapper.deleteEmergencyByTroubleIds(troubleIds);
        return webSafeTroubleMapper.deleteWebSafeTroubleByTroubleIds(troubleIds);
    }

    /**
     * 删除网络安全事故信息
     * 
     * @param troubleId 网络安全事故主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWebSafeTroubleByTroubleId(Integer troubleId)
    {
        webSafeTroubleMapper.deleteEmergencyByTroubleId(troubleId);
        return webSafeTroubleMapper.deleteWebSafeTroubleByTroubleId(troubleId);
    }

    /**
     * 新增应急措施信息
     * 
     * @param webSafeTrouble 网络安全事故对象
     */
    public void insertEmergency(WebSafeTrouble webSafeTrouble)
    {
        List<Emergency> emergencyList = webSafeTrouble.getEmergencyList();
        Integer troubleId = webSafeTrouble.getTroubleId();
        if (StringUtils.isNotNull(emergencyList))
        {
            List<Emergency> list = new ArrayList<Emergency>();
            for (Emergency emergency : emergencyList)
            {
                emergency.setTroubleId((long)troubleId);
                list.add(emergency);
            }
            if (list.size() > 0)
            {
                webSafeTroubleMapper.batchEmergency(list);
            }
        }
    }
}
